package at.ac.univie.zsu.aguataplan.ui;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.EventAttribute;
import at.ac.univie.zsu.aguataplan.domain.EventAttributeValueString;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.service.EventManager;
import at.ac.univie.zsu.aguataplan.ui.template.IDialogListEntry;
import at.ac.univie.zsu.aguataplan.ui.template.ListEntryPanel;

/**
 * @author gerry
 * 
 */
public class DialogEvent extends JDialog implements UIConstants {
	// private static Logger log = Logger.getLogger(DialogEvent.class);

	private static final long serialVersionUID = 1L;

	private JFrame parent;
	private GeneralProperties generalProperties;
	private I18nProperties i18nProp;

	private EventManager eventManager;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DialogEvent(MainWindow parent, GeneralProperties generalProperties) {
		super(parent, true);

		this.parent = parent;
		this.generalProperties = generalProperties;
		this.i18nProp = generalProperties.getI18nProperties();
		this.eventManager = parent.getServices().getEventManager();

		this.setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setLocationByPlatform(true);
		setSize(SPACING + PANEL_SIZE_X_ENTRY + SPACING + PANEL_SIZE_X_ENTRY + SPACING + PANEL_SIZE_X_ENTRY + SPACING
				+ SPACING, SPACING + PANEL_SIZE_Y_ENTRY + SPACING + DIALOG_SIZE_Y_HEADER);
		setTitle(i18nProp.getString(EVENT_HEADER));
		setResizable(false);

		addEntries();
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void addEntries() {
		// event panel
		ListEntryPanel eventPanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<String> select(Object obj) {
				Event event = (Event) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(event.getName()));
				list.add(new String(event.getDescription()));
				// list.add(new String(event.getShortcut()));
				return list;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				Event event = (Event) obj;
				event.setName(list.get(0));
				event.setDescription(list.get(1));
				// event.setShortcut(list.get(2));

				Event ev = null;

				try {
					ev = eventManager.mergeEvent(event);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_SAVE_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}

				return ev;
			}

			@Override
			public List<?> initText() {
				List<Event> events = null;
				try {
					events = eventManager.getEventList();
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_LIST_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}
				return events;
			}

			@Override
			public Object delete(Object obj) {
				Event event = (Event) obj;

				try {
					eventManager.deleteEvent(event.getId());
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_DELETE_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
					return null;
				}

				return event;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				Event event = null;
				try {
					// event = eventManager.addEvent(list.get(0), list.get(1),
					// list.get(2));
					event = eventManager.addEvent(list.get(0), list.get(1), "");
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_ADD_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}
				return event;
			}

			@Override
			public List<List<?>> initCombo() {
				return null;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				return null;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}

		});
		List<String> listEvent = new ArrayList<String>();
		listEvent.add(new String(i18nProp.getString(EVENT_NAME)));
		listEvent.add(new String(i18nProp.getString(EVENT_DESCRIPTION)));
		// listEvent.add(new String(i18nProp.getString(EVENT_SHORTCUT)));
		eventPanel.init(i18nProp.getString(EVENT_BORDER), PANEL_SIZE_X_ENTRY, PANEL_SIZE_Y_ENTRY, listEvent,
				new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(),
				new ArrayList<ActionListener>());

		// attribute panel
		ListEntryPanel attributePanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<String> select(Object obj) {
				EventAttribute eventAttribute = (EventAttribute) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(eventAttribute.getName()));
				// list.add(new String(eventAttribute.getShortcut()));
				return list;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				EventAttribute eventAttribute = (EventAttribute) obj;
				eventAttribute.setName(list.get(0));
				// eventAttribute.setShortcut(list.get(1));

				EventAttribute ea = null;

				try {
					ea = eventManager.mergeEventAttribute(eventAttribute);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_SAVE_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}

				return ea;
			}

			@Override
			public List<?> initText() {
				List<EventAttribute> eventAttributes = null;
				try {
					eventAttributes = eventManager.getEventAttributeList();
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_LIST_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}
				return eventAttributes;
			}

			@Override
			public Object delete(Object obj) {
				EventAttribute eventAttribute = (EventAttribute) obj;

				try {
					eventManager.deleteEventAttribute(eventAttribute.getId());
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_DELETE_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
					return null;
				}

				return eventAttribute;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				EventAttribute eventAttribute = null;
				try {
					// eventAttribute =
					// eventManager.addEventAttribute(list.get(0), list.get(1));
					eventAttribute = eventManager.addEventAttribute(list.get(0), "");
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_ADD_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}
				return eventAttribute;
			}

			@Override
			public List<List<?>> initCombo() {
				return null;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				return null;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}

		});
		List<String> listAttribute = new ArrayList<String>();
		listAttribute.add(new String(i18nProp.getString(EVENT_ATTRIBUTENAME)));
		// listAttribute.add(new
		// String(i18nProp.getString(EVENT_ATTRIBUTESHORTCUT)));
		attributePanel.init(i18nProp.getString(EVENT_ATTRIBUTEBORDER), PANEL_SIZE_X_ENTRY, PANEL_SIZE_Y_ENTRY,
				listAttribute, new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(),
				new ArrayList<ActionListener>());

		// attributeValueString panel
		ListEntryPanel attributeValueStringPanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<String> select(Object obj) {
				EventAttributeValueString eventAttributeValueString = (EventAttributeValueString) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(eventAttributeValueString.getAttValue()));
				return list;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				EventAttributeValueString eventAttributeValueString = (EventAttributeValueString) obj;
				eventAttributeValueString.setAttValue(list.get(0));

				EventAttributeValueString eavs = null;

				try {
					eavs = eventManager.mergeEventAttributeValueString(eventAttributeValueString);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_SAVE_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}

				return eavs;
			}

			@Override
			public List<?> initText() {
				List<EventAttributeValueString> eventAttributeValueStrings = null;
				try {
					eventAttributeValueStrings = eventManager.getEventAttributeValueStringList();
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_LIST_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}
				return eventAttributeValueStrings;
			}

			@Override
			public Object delete(Object obj) {
				EventAttributeValueString eventAttributeValueString = (EventAttributeValueString) obj;

				try {
					eventManager.deleteEventAttributeValueString(eventAttributeValueString.getId());
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_DELETE_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
					return null;
				}

				return eventAttributeValueString;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				EventAttributeValueString eventAttributeValueString = null;
				try {
					eventAttributeValueString = eventManager.addEventAttributeValueString(list.get(0));
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogEvent.this, i18nProp.getString(EX_EVENT_ADD_DESC),
							i18nProp.getString(EX_EVENT), JOptionPane.ERROR_MESSAGE);
				}
				return eventAttributeValueString;
			}

			@Override
			public List<List<?>> initCombo() {
				return null;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				return null;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}

		});

		List<String> listAttributeValueString = new ArrayList<String>();
		listAttributeValueString.add(new String(i18nProp.getString(EVENT_ATTRIBUTEVALUEATTVALUE)));
		attributeValueStringPanel.init(i18nProp.getString(EVENT_ATTRIBUTEVALUEBORDER), PANEL_SIZE_X_ENTRY,
				PANEL_SIZE_Y_ENTRY, listAttributeValueString, new ArrayList<String>(), new ArrayList<String>(),
				new ArrayList<String>(), new ArrayList<ActionListener>());

		// add to dialog
		eventPanel.setLocation(SPACING, SPACING);
		attributePanel.setLocation(SPACING + PANEL_SIZE_X_ENTRY + SPACING, SPACING);
		attributeValueStringPanel.setLocation(SPACING + PANEL_SIZE_X_ENTRY + SPACING + PANEL_SIZE_X_ENTRY + SPACING,
				SPACING);
		this.add(eventPanel);
		this.add(attributePanel);
		this.add(attributeValueStringPanel);

		// finish init
		eventPanel.finishInit();
		attributePanel.finishInit();
		attributeValueStringPanel.finishInit();
	}
}
